import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QRadioButton


class MainWindow(QWidget):#继承QWidget
    def __init__(self, *args, **kwargs):#self is default parameter;you can add other parameters;
        #*args is mean any parameters; *kwargs is mean any dictronary parameters;
        super().__init__()#继承执行父类的构造函数
        self.setWindowTitle("radiobutton") #set window title;be overrides the title
        ###############################################
        self.resize(400,300)#
        layout = QVBoxLayout()
        lable = QLabel("选择地区电影:")
        rd1 = QRadioButton("中国") #创建一个单选按钮,一组里只能选一个
        rd1.setChecked(True) #设置默认选中
        rd1.toggled.connect(self.choise)#变化就执行
        rd2 = QRadioButton("美国")
        rd2.toggled.connect(self.choise)
        rd3 = QRadioButton("印度")
        rd3.toggled.connect(self.choise)
        layout.addWidget(lable)
        layout.addWidget(rd1)
        layout.addWidget(rd2)
        layout.addWidget(rd3)
        self.setLayout(layout)#把布局添加到窗口中
        ###############################################
        bg_color = kwargs.get('setbackgroundColor', None)
        if bg_color:
            self.setStyleSheet(f"background-color: {bg_color};")
        self.show()
    def choise(self):
        if self.sender().isChecked():
            print(self.sender().text())#选中就打印,撤离不打印


if __name__ == "__main__":
    #print(sys.argv) #['D:\\u\\test\\gitee\\pythonqt\\test1\\改造程序\\new_hello2.py'] print sys.argv;default is this file path.
    app=QApplication(sys.argv)#get parameters from command line.
    w=MainWindow(setbackgroundColor="lightblue") #use MainWindow created a window.
    w.setWindowTitle("radiobutton")
    w.show()#显示窗口
    #app.exec()#运行程序

    sys.exit(app.exec()) #app.exec() is first stop,then out process

    #import time #test app end,but process is not stop.
    #for i in range(10):#genarate a dictionary
    #    print(i)
    #    time.sleep(1)
# app=QApplication([])
# window=QWidget(windowTitle="Hello World",)#创建一个窗口
# window.show()#显示窗口
# app.exec()#运行程序
